//
//  QuWeiXunLianAnimationViewController.swift
//  CKC
//
//  Created by 张振东 on 16/10/5.
//  Copyright © 2016年 ZZD. All rights reserved.
//

import UIKit

class QuWeiXunLianAnimationViewController: UIViewController {

    let screenWidth = UIScreen.main.bounds.width
    let screenHeight = UIScreen.main.bounds.height
    var navigationHeight:CGFloat?
    var xianShiMoshiView:UIView?
    var lianXiMoshiView:UIView?
    var xuanXiangView:UIView?
    var xianShiMoshiLabel:UILabel?
    var lianXiMoshiLabel:UILabel?
    var xuanXiangLabel:UILabel?
    
    override func viewDidLoad() {
        self.title = "趣味训练"
        
        self.view.backgroundColor = UIColor(red: 246,green: 246,blue: 246,alpha: 1)
        let leftBarButton = UIButton(frame:CGRect(x: 0,y: 0,width: 20,height: 20))
        leftBarButton.setBackgroundImage(UIImage(named: "fanhui"), for: UIControlState())
        leftBarButton.addTarget(self, action: #selector(backToPrevious), for: .touchUpInside)
        let leftBarButtonItem = UIBarButtonItem(customView: leftBarButton)
        self.navigationItem.leftBarButtonItems = [leftBarButtonItem]
        
        //获取导航栏的高度
        navigationHeight = self.navigationController?.navigationBar.bounds.size.height
        
        //初始化限时模式的UIView对象
        xianShiMoshiView = UIView(frame:CGRect(x: 30,y: navigationHeight!+50,width: 100,height: 100))
        xianShiMoshiView?.backgroundColor = UIColor(red: 155/255.0,green:89/255.0,blue:182/255.0,alpha:1)
        xianShiMoshiView?.layer.cornerRadius = 50
        
        //初始化练习模式的UIView对象
        lianXiMoshiView = UIView(frame:CGRect(x: screenWidth-100,y: navigationHeight!+50+100+30,width: 100,height: 100))
        lianXiMoshiView?.backgroundColor = UIColor(red: 149/255.0,green:165/255.0,blue:166/255.0,alpha:1)
        lianXiMoshiView?.layer.cornerRadius = 50
        
        
        //初始化选项模式的UIView对象
        xuanXiangView = UIView(frame:CGRect(x: 30,y: navigationHeight!+50+100+30+100+30,width: 100,height: 100))
        xuanXiangView?.backgroundColor = UIColor(red: 26/255.0,green:188/255.0,blue:156/255.0,alpha:1)
        xuanXiangView?.layer.cornerRadius = 50
        
        //初始化限时模式的UILabel对象
        xianShiMoshiLabel = UILabel(frame:CGRect(x: 0,y: (100-40)/2,width: 100,height: 40))
        xianShiMoshiLabel?.text = "限时模式"
        xianShiMoshiView?.insertSubview(xianShiMoshiLabel!, at: 0)
        xianShiMoshiLabel?.textAlignment = .center
        
        //初始化限练习模式的UILabel对象
        lianXiMoshiLabel = UILabel(frame:CGRect(x: 0,y: (100-40)/2,width: 100,height: 40))
        lianXiMoshiLabel?.text = "练习模式"
        lianXiMoshiLabel?.textAlignment = .center
        lianXiMoshiView?.addSubview(lianXiMoshiLabel!)
        
        xuanXiangLabel = UILabel(frame:CGRect(x: 0,y: (100-40)/2,width: 100,height: 40))
        xuanXiangLabel?.text = "选项"
        xuanXiangLabel?.textAlignment = .center
        xuanXiangView?.addSubview(xuanXiangLabel!)
        
        UIView.animate(withDuration: 0.3, delay: 0.3, options: UIViewAnimationOptions.curveEaseOut, animations: {
            //限时模式动画
            self.xianShiMoshiView?.frame = CGRect(x:150,y:self.navigationHeight!+50,width:100,height:100)
            self.xianShiMoshiView?.backgroundColor = UIColor.gray
            
            //练习模式动画
            self.lianXiMoshiView?.frame = CGRect(x: self.screenWidth-100,y: self.navigationHeight!+50+100+30,width: 100,height: 100)
            self.lianXiMoshiView?.backgroundColor = UIColor.brown
            
            //选项动画
            self.xuanXiangView?.frame = CGRect(x: 30,y: self.navigationHeight!+50+100+30+100+30,width: 100,height: 100)
            self.xuanXiangView?.backgroundColor = UIColor(red: 26/255.0,green:188/255.0,blue:156/255.0,alpha:1)
            
        }) { (finished) in
            UIView.animate(withDuration: 0.3, delay: 0.3, options: UIViewAnimationOptions.curveEaseOut, animations: {
                //限时模式动画
                self.xianShiMoshiView?.frame = CGRect(x:180,y:self.navigationHeight!+50,width:120,height:120)
                self.xianShiMoshiView?.backgroundColor = UIColor.red
                //练习模式动画
                self.lianXiMoshiView?.frame = CGRect(x: 30,y: self.navigationHeight!+50+100+30,width: 100,height: 100)
                self.lianXiMoshiView?.backgroundColor = UIColor.green
                //选项动画
                self.xuanXiangView?.frame = CGRect(x:180,y:self.navigationHeight!+50,width:100,height:100)
                self.xuanXiangView?.backgroundColor = UIColor.gray
                
                
            }, completion: { (finished) in
              UIView.animate(withDuration: 0.3, delay: 0.3, options: UIViewAnimationOptions.curveEaseOut, animations: {
                //限时模式动画
                self.xianShiMoshiView?.frame = CGRect(x:150,y:self.navigationHeight!+50,width:100,height:100)
                self.xianShiMoshiView?.backgroundColor = UIColor(red: 155/255.0,green:89/255.0,blue:182/255.0,alpha:1)
                //练习模式动画
                self.lianXiMoshiView?.frame = CGRect(x: 50,y: self.navigationHeight!+50+100+30,width: 100,height: 100)
                self.lianXiMoshiView?.backgroundColor = UIColor(red: 149/255.0,green:165/255.0,blue:166/255.0,alpha:1)
                //选项动画
                self.xuanXiangView?.frame = CGRect(x: 150,y: self.navigationHeight!+50+100+30+100+30,width: 100,height: 100)

                self.xuanXiangView?.backgroundColor = UIColor(red: 26/255.0,green:188/255.0,blue:156/255.0,alpha:1)
                
              }, completion: { (finished) in
                print("阿东你好")
              })
            })
        }
//        //创建JNM实例
//        let scale = JNWSpringAnimation(keyPath:"transform.scale")
//        //阻尼
//        scale?.damping = 6
//        
//        //伸展系数,弹簧伸缩的难度
//        scale?.stiffness = 200
//        //质量
//        scale?.mass = 2
//        //设置初始值
//        scale?.fromValue = 3
//        //设置结束值
//        scale?.toValue = 1
//        //给xianShiMoshiView的层添加动画
////        xianShiMoshiView?.layer.add(scale!, forKey: scale?.keyPath)
////        
////        //更新发生的变化
////        xianShiMoshiView?.transform = CGAffineTransform(scaleX: 1, y: 1)
//
//        //给lianXiMoshiView的层添加动画
//        lianXiMoshiView?.layer.add(scale!, forKey: scale?.keyPath)
//        //给新lianXiMoshiView发生的变化
//        lianXiMoshiView?.transform = CGAffineTransform(scaleX: 1, y: 1)
//
//        //给xuanXiangView的层添加动画
//        xuanXiangView?.layer.add(scale!, forKey: scale?.keyPath)
//        xuanXiangView?.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
//
//        
//        //创建JNW位移对象
//        let weiYi = JNWSpringAnimation(keyPath:"transform.translation.x")
//        weiYi?.damping = 6
//        weiYi?.stiffness = 100
//        weiYi?.mass = 2
//        //设置初始值
//        weiYi?.fromValue = 0
//        weiYi?.toValue = 160
//        //为xianShiMoshiView添加动画
//        xianShiMoshiView?.layer.add(weiYi!, forKey: weiYi?.keyPath)
//        xianShiMoshiView?.transform = CGAffineTransform(translationX: 160, y: 0)
//
//        //为xuanXiangView添加动画
//        xuanXiangView?.layer.add(weiYi!, forKey: weiYi?.keyPath)
//        xuanXiangView?.transform = CGAffineTransform(translationX: 160, y: 0)
//
//        
//        //创建JNW位移对象
//        let lianXiMoshiWeiYi = JNWSpringAnimation(keyPath:"transform.translation.x")
//        lianXiMoshiWeiYi?.damping = 6
//        lianXiMoshiWeiYi?.stiffness = 100
//        lianXiMoshiWeiYi?.mass = 2
//        //设置初始值
//        lianXiMoshiWeiYi?.fromValue = 0
//        lianXiMoshiWeiYi?.toValue = -160
//        lianXiMoshiView?.layer.add(lianXiMoshiWeiYi!, forKey: lianXiMoshiWeiYi?.keyPath)
//        lianXiMoshiView?.transform = CGAffineTransform(translationX: -160, y: 0)
        
        let xianShiMoShiTapGesture = UITapGestureRecognizer(target: self,action: #selector(handleXianShiMoShi))
        let lianXiMoShiTapGesture = UITapGestureRecognizer(target: self,action: #selector(handleLianXiMoShi))
        let xuanXiangMoShiTapGesture = UITapGestureRecognizer(target: self,action: #selector(handleXuanXiang))
        xianShiMoshiView?.addGestureRecognizer(xianShiMoShiTapGesture)
        lianXiMoshiView?.addGestureRecognizer(lianXiMoShiTapGesture)
        xuanXiangView?.addGestureRecognizer(xuanXiangMoShiTapGesture)

        self.view.addSubview(xianShiMoshiView!)
        self.view.addSubview(lianXiMoshiView!)
        self.view.addSubview(xuanXiangView!)
        
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    
    override func viewWillAppear(_ animated: Bool) {
        //隐藏导航栏
        self.navigationController?.navigationBar.isHidden = false
    }
    
    //跳转到限时模式界面
    func handleXianShiMoShi(){
        let quWeiXunLianMainViewController = QuWeiXunLianMainViewController()
        self.navigationController?.pushViewController(quWeiXunLianMainViewController, animated: true)
    }
    
    
    //跳转到练习模式界面
    func handleLianXiMoShi(){
        let quWeiGameViewController = QuWeiGameViewController()
        self.navigationController?.pushViewController(quWeiGameViewController, animated: true)
    }
    
    //跳转到选项界面
    func handleXuanXiang(){
        let quWeiXunLianXuanXiangViewController = QuWeiXunLianXuanXiangViewController()
        self.navigationController?.pushViewController(quWeiXunLianXuanXiangViewController, animated: true)
        
    }
    
    
    //返回到上一个页面
    func backToPrevious(){
        self.navigationController?.popViewController(animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}
